package com.zqxq.file.utils;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * Created by Administrator on 2016/6/17 0017.
 * 线程池
 */
public class MyThreadPoolExecutor {
    public static void main(String[] args) {
        System.out.println(normalThreadNum);
        System.out.println(maxThreadNum);
    }

    //添加任务到线程池
    public static void add(Runnable runnable) {
        threadpool.execute(runnable);
    }

    // 核心线程
    private static int normalThreadNum = Runtime.getRuntime().availableProcessors();
    // 最大线程，处理器的5～10倍
    private static int maxThreadNum = Runtime.getRuntime().availableProcessors() * 10;
    // 业务空闲超时时间
    private static int spanTime = 30;
    // 任务队列大小
    private static int queueSize = maxThreadNum;
    //线程池执行器     核心线程corePoolSize、任务队列workQueue、最大线程maximumPoolSize，如果三者都满了，使用handler处理被拒绝的任务
    private static ThreadPoolExecutor threadpool = new ThreadPoolExecutor(
            normalThreadNum,
            maxThreadNum,
            spanTime,
            TimeUnit.SECONDS,
            new ArrayBlockingQueue<Runnable>(queueSize),
            new ThreadPoolExecutor.CallerRunsPolicy());
}
